//
//  TagSettingViewController.m
//  MoneyBook
//
//  Created by Justin Kim on 11. 6. 27..
//  Copyright 2011 Daum. All rights reserved.
//

#import "TagSettingViewController.h"


@implementation TagSettingViewController
/////////////////////////////////////////////////////////////////////////////////////////////////
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    }   
    return self;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)dealloc{
    [super dealloc];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)didReceiveMemoryWarning{
    [super didReceiveMemoryWarning];
}

#pragma mark - View lifecycle
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidLoad{
    self.title = @"태그 관리";
    [self.tableView setAllowsSelectionDuringEditing:YES];
     
    [super viewDidLoad];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewDidUnload{
    [super viewDidUnload];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.dataSourceArray = [[SQLHandler sharedInstance] findAllTag];    
    [self.tableView reloadData];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{
	[super shouldAutorotateToInterfaceOrientation:interfaceOrientation];
    return YES;
}

/////////////////////////////////////////////////////////////////////////////////////////////////
- (void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
    [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration];
    
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)showEditMode{
    _isEditMode = YES;
    [self.tableView setEditing:YES animated:YES];
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                                                              target:self
                                                                              action:@selector(showDoneMode)];
    [self.navigationItem setRightBarButtonItem:rightBtn];        
    [rightBtn release];
}

#pragma mark -
#pragma mark UIBarButtonItemDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)addAction{
    TagFormViewController* viewController = [[[TagFormViewController alloc] initWithNibName:@"TagFormView" bundle:nil] autorelease];
    [[self navigationController] pushViewController:viewController animated:YES];        
    [viewController createMode];
}
#pragma mark -
#pragma mark UITableViewDelegate
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    TagFormViewController* viewController = [[[TagFormViewController alloc] initWithNibName:@"TagFormView" bundle:nil] autorelease];
    [[self navigationController] pushViewController:viewController animated:YES];        
    [viewController loadTag:[[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"tag_id"]];        
}
/////////////////////////////////////////////////////////////////////////////////////////////////
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    SQLHandler *sql = [SQLHandler sharedInstance];
    [sql deleteTagById:[[_dataSourceArray objectAtIndex:indexPath.row] objectForKey:@"tag_id"]];
    self.dataSourceArray = [sql findAllTag];
    [self.tableView reloadData];
}
/////////////////////////////////////////////////////////////////////////////////////////////////
-(void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath{
    [self moveRowAtIndexPath:fromIndexPath
                 toIndexPath:toIndexPath 
                   tableName:@"tag"];
}
@end
